
package com.dream.utils.io;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

import com.dream.utils.log.DreamLogUtils;

/**
 * @copyright evan
 * @author evan
 * @Revision
 * @date 2012/09/01
 */
public class FileIOUtils {
	
	private static String SALASH = "/";
	/**
	 * get the file input stream 
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static InputStream fileInputStream(String fileName) {
		
		InputStream in = null;
		File file = new File(fileName);
		if (file.exists()) {
			try {
				in = new FileInputStream(file);
				
			} catch (FileNotFoundException e) {
				DreamLogUtils.error(FileIOUtils.class, "fileInputStream", "", e);
				throw new RuntimeException(e);
			}
		} else {
			in = FileIOUtils.class.getClassLoader().getResourceAsStream(fileName);
		}
		return in;
		
	}
	
	public static BufferedInputStream fileBufferedInputStream(String fileName) {
		
		return new BufferedInputStream(fileInputStream(fileName));
		
	}
	
	/**
	 * get the file input stream reader
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static InputStreamReader fileInputStreamReader(String fileName){
		
		return new InputStreamReader(fileInputStream(fileName));
		
	}

	/**
	 * get the file buffered reader
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static BufferedReader fileBufferedReader(String fileName){
		
		return new BufferedReader(fileInputStreamReader(fileName));
	}
	
	/**
	 * get the file output stream
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static OutputStream fileOutputStream(String fileName){
		
		OutputStream out = null;
		File file = new File(fileName);
		
		//file not exist 
		if (!file.exists()) {
			
			//just a file name , create it in class root
			if(!fileName.contains(File.separator) && !fileName.contains(SALASH)){
				fileName = new StringBuilder(FileIOUtils.class.getClassLoader().getResource("").getPath())
				.append(File.separator)
				.append(fileName).toString();
			}
			
			file = new File(fileName);
			try {
				forceMakeDir(file);
				file.createNewFile();
			} catch (IOException e) {
				DreamLogUtils.error(FileIOUtils.class, "fileOutputStream", "", e);
				throw new RuntimeException(e);
			}
			
		}
		
		try {
			out = new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			DreamLogUtils.error(FileIOUtils.class, "fileOutputStream", "", e);
			throw new RuntimeException(e);
		}
		return out;
		
	}
	
	/**
	 * get the file output stream writer
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static OutputStreamWriter fileOutputStreamWriter(String fileName) throws IOException{
		
		return new OutputStreamWriter(fileOutputStream(fileName));
	}
	
	/**
	 * get the file output buffered writer
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static BufferedWriter fileBufferedWriter(String fileName) throws IOException{
		
		return new BufferedWriter(fileOutputStreamWriter(fileName));
	}
	
	/**
	 * close the input stream
	 * @param in
	 * @throws IOException
	 */
	public static void close(InputStream in) {

		if (null == in) {
			return;
		}

		try {
			in.close();
		} catch (IOException e) {
			DreamLogUtils.error(FileIOUtils.class, "close", "", e);
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * close the reader
	 * @param reader
	 * @throws IOException
	 */
	public static void close(Reader reader) {

		if (null == reader) {
			return;
		}
		try {
			reader.close();
		} catch (IOException e) {
			DreamLogUtils.error(FileIOUtils.class, "close", "", e);
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * close the output stream
	 * @param out
	 * @throws IOException
	 */
	public static void close(OutputStream out){

		if (null == out) {
			return;
		}

		try {
			out.flush();
			out.close();
		} catch (IOException e) {
			DreamLogUtils.error(FileIOUtils.class, "close", "", e);
			throw new RuntimeException(e);
		}
		
	}
	
	/**
	 * close the writer
	 * @param writer
	 * @throws IOException
	 */
	public static void close(Writer writer){

		if (null == writer) {
			return;
		}
		
		try {
			writer.flush();
			writer.close();
		} catch (IOException e) {
			DreamLogUtils.error(FileIOUtils.class, "close", "", e);
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * force make directory
	 * @param file
	 * @return
	 */
	public static boolean forceMakeDir(File file){

		boolean isSucc = true;
		if (!file.exists()) {
			if (file.getParentFile() != null && !(file.getParentFile().exists())) {
				isSucc = file.getParentFile().mkdirs();
			}
		}
		return isSucc;
	}
}
